#pragma once
#include <cstdlib>
#include <iostream>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
#include <memory>
#include "logger.hpp"
namespace zzy_mims
{
class DMSClient
{
public:
    using ptr = std::shared_ptr<DMSClient>;
    DMSClient(const std::string& AccessKeyID,const std::string& AccessKeySecret)
    {
        AlibabaCloud::InitializeSdk();
        AlibabaCloud::ClientConfiguration configuration( "cn-qingdao" );
        configuration.setConnectTimeout(1500);
        configuration.setReadTimeout(4000);
        AlibabaCloud::Credentials credential(AccessKeyID,AccessKeySecret);
        _client = std::make_unique<AlibabaCloud::CommonClient>(credential,configuration);
    };
    ~DMSClient(){AlibabaCloud::ShutdownSdk();};
    bool send(const std::string& PhoneNumbers,const std::string& code)
    {
        AlibabaCloud::CommonRequest request(AlibabaCloud::CommonRequest::RequestPattern::RpcPattern);
        request.setHttpMethod(AlibabaCloud::HttpRequest::Method::Post);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setQueryParameter("Action", "SendSms");
        request.setQueryParameter("SignName", "阿里云短信测试");
        request.setQueryParameter("TemplateCode", "SMS_154950909");
        request.setQueryParameter("PhoneNumbers", PhoneNumbers);
        request.setQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        auto response = _client->commonResponse(request);
        if (!response.isSuccess()) 
        {
            LOG_ERROR("向{}发送的短信验证码{}失败",PhoneNumbers,code);
            return false;
        }
        return true;
    }
private:
    std::unique_ptr<AlibabaCloud::CommonClient> _client;
};
}